"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : HA主备高可用
Case Name   : ignore_standby_lsn_window参数异常设置场景验证
Create At   : 2024-4-10
Owner       : @peilinqian
Description :
    1、gs_guc设置ignore_standby_lsn_window，设置值为小数；
    2、gs_guc设置ignore_standby_lsn_window，设置单位不正确；
    3、gs_guc设置ignore_standby_lsn_window，设置带单位超出最大值；
    4、gs_guc设置ignore_standby_lsn_window，设置不带单位超出最大值；
    5、gs_guc设置ignore_standby_lsn_window，设置值为负数；
    6、alter system方式设置ignore_standby_lsn_window，设置值为小数；
    7、alter system方式设置ignore_standby_lsn_window，设置单位不正确；
    8、alter system方式设置ignore_standby_lsn_window，设置带单位超出最大值；
    9、alter system方式设置ignore_standby_lsn_window，设置带不单位超出最大值；
    10、alter system方式设置ignore_standby_lsn_window，设置值为负数；
    11、修改配置文件方式设置ignore_standby_lsn_window，设置异常值，超出范围后重启；expect：
    12、修改配置文件方式设置ignore_standby_lsn_window，设置单位不正确；expect：
Expect      :
    1、gs_guc设置ignore_standby_lsn_window，设置值为小数；失败
    2、gs_guc设置ignore_standby_lsn_window，设置单位不正确；失败
    3、gs_guc设置ignore_standby_lsn_window，设置带单位超出最大值；失败
    4、gs_guc设置ignore_standby_lsn_window，设置不带单位超出最大值；失败
    5、gs_guc设置ignore_standby_lsn_window，设置值为负数；失败
    6、alter system方式设置ignore_standby_lsn_window，设置值为小数；失败
    7、alter system方式设置ignore_standby_lsn_window，设置单位不正确；失败
    8、alter system方式设置ignore_standby_lsn_window，设置带单位超出最大值；失败
    9、alter system方式设置ignore_standby_lsn_window，设置带不单位超出最大值；失败
    10、alter system方式设置ignore_standby_lsn_window，设置值为负数；失败
    11、修改配置文件方式设置ignore_standby_lsn_window，设置异常值，超出范围后重启；expect：失败
    12、修改配置文件方式设置ignore_standby_lsn_window，设置单位不正确；expect：失败
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class HaPrimary(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}: start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_oguser = Node(node='PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.err_flag1 = '.*: The value .* is incorrect, ' \
                         'requires a integer value'
        self.err_flag2 = '.*Valid units for this parameter .*are ' \
                         '"ms", "s", "min", "h", and "d".'
        self.err_flag3 = '.* is outside the valid range for ' \
                         '.*(0 .. 2147483647).*'
        self.err_flag4 = '.*  invalid value for parameter .*'
        self.err_flag5 = 'HINT:  Value exceeds integer range.'
        self.err_flag6 = 'FATAL:  configuration file ".*/postgresql.conf" ' \
                         'contains errors'

    def test_main(self):
        step_txt = '----查询ignore_standby_lsn_window默认值----'
        self.log.info(step_txt)
        self.init_para = self.common.show_param('ignore_standby_lsn_window')

        step_txt = '----step1：gs_guc设置ignore_standby_lsn_window，设置值为小数； ' \
                   'expect：失败----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window=120.5d",
                                           get_detail=True, single=True)
        self.assertRegex(result, self.err_flag1, '执行失败:' + step_txt)

        step_txt = '----step2：gs_guc设置ignore_standby_lsn_window，设置单位不正确； ' \
                   'expect：失败----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window=120m",
                                           get_detail=True, single=True)
        self.assertRegex(result, self.err_flag2, '执行失败:' + step_txt)

        step_txt = '----step3：gs_guc设置ignore_standby_lsn_window，设置带单位超出最大值； ' \
                   'expect：失败----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window= 25d",
                                           get_detail=True, single=True)
        self.assertRegex(result, self.err_flag3, '执行失败:' + step_txt)

        step_txt = '----step4：gs_guc设置ignore_standby_lsn_window，设置不带单位超出最大值； ' \
                   'expect：失败----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window= "
                                           "2147483648", get_detail=True,
                                           single=True)
        self.assertRegex(result, self.err_flag3, '执行失败:' + step_txt)

        step_txt = '----step5：gs_guc设置ignore_standby_lsn_window，设置值为负数； ' \
                   'expect：失败----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc("reload",
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "ignore_standby_lsn_window= -1",
                                           get_detail=True, single=True)
        self.assertRegex(result, self.err_flag3, '执行失败:' + step_txt)

        step_txt = '----step6: alter system方式设置ignore_standby_lsn_window，' \
                   '设置值为小数；expect： 失败----'
        self.log.info(step_txt)
        alter_sql = "alter system set ignore_standby_lsn_window='13.1s' ;"
        alter_result = self.pri_sh.execut_db_sql(alter_sql)
        self.log.info(alter_result)
        self.assertRegex(alter_result, self.err_flag4, '执行失败:' + step_txt)

        step_txt = '----step7: alter system方式设置ignore_standby_lsn_window，' \
                   '设置单位不正确；expect： 失败----'
        self.log.info(step_txt)
        alter_sql = "alter system set ignore_standby_lsn_window='120m' ;"
        alter_result = self.pri_sh.execut_db_sql(alter_sql)
        self.log.info(alter_result)
        self.assertRegex(alter_result, self.err_flag4, '执行失败:' + step_txt)

        step_txt = '----step8: alter system方式设置ignore_standby_lsn_window，' \
                   '设置带单位超出最大值；expect： 失败----'
        self.log.info(step_txt)
        alter_sql = "alter system set ignore_standby_lsn_window='25d' ;"
        alter_result = self.pri_sh.execut_db_sql(alter_sql)
        self.log.info(alter_result)
        self.assertRegex(alter_result, self.err_flag5, '执行失败:' + step_txt)

        step_txt = '----step9: alter system方式设置ignore_standby_lsn_window，' \
                   '设置带不单位超出最大值；expect： 失败----'
        self.log.info(step_txt)
        alter_sql = "alter system set ignore_standby_lsn_window='2147483648' ;"
        alter_result = self.pri_sh.execut_db_sql(alter_sql)
        self.log.info(alter_result)
        self.assertRegex(alter_result, self.err_flag5, '执行失败:' + step_txt)

        step_txt = '----step10: alter system方式设置ignore_standby_lsn_window，' \
                   '设置值为负数；expect： 失败----'
        self.log.info(step_txt)
        alter_sql = "alter system set ignore_standby_lsn_window='-11' ;"
        alter_result = self.pri_sh.execut_db_sql(alter_sql)
        self.log.info(alter_result)
        self.assertRegex(alter_result, self.err_flag3, '执行失败:' + step_txt)

        step_txt = '----step11: 修改配置文件方式设置ignore_standby_lsn_window，' \
                   '设置异常值，超出范围后重启；expect：失败----'
        self.log.info(step_txt)
        cmd = f"echo 'ignore_standby_lsn_window=2147483648' >>" \
              f"{macro.DB_INSTANCE_PATH}/postgresql.conf"
        self.log.info(cmd)
        result = self.pri_oguser.sh(cmd).result()
        self.log.info(result)
        restart_result = self.pri_sh.restart_db_cluster(get_detail=True)
        restore_cmd = f"sed -i '/ignore_standby_lsn_window/d' " \
                      f"{macro.DB_INSTANCE_PATH}/postgresql.conf;" \
                      f"grep 'ignore_standby_lsn_window' " \
                      f"{macro.DB_INSTANCE_PATH}/postgresql.conf;"
        self.log.info(restore_cmd)
        restore_result = self.pri_oguser.sh(restore_cmd).result()
        self.log.info(restore_result)
        self.assertRegex(restart_result, self.err_flag4, '执行失败:' + step_txt)
        self.assertRegex(restart_result, self.err_flag5, '执行失败:' + step_txt)
        self.assertRegex(restart_result, self.err_flag6, '执行失败:' + step_txt)

        step_txt = '----step12: 修改配置文件方式设置ignore_standby_lsn_window，' \
                   '设置单位不正确；expect：失败----'
        self.log.info(step_txt)
        cmd = f"echo 'ignore_standby_lsn_window=1m' >>" \
              f"{macro.DB_INSTANCE_PATH}/postgresql.conf"
        self.log.info(cmd)
        result = self.pri_oguser.sh(cmd).result()
        self.log.info(result)
        restart_result = self.pri_sh.restart_db_cluster(get_detail=True)
        self.log.info(restore_cmd)
        restore_result = self.pri_oguser.sh(restore_cmd).result()
        self.log.info(restore_result)
        self.assertRegex(restart_result, self.err_flag2, '执行失败:' + step_txt)
        self.assertRegex(restart_result, self.err_flag4, '执行失败:' + step_txt)
        self.assertRegex(restart_result, self.err_flag6, '执行失败:' + step_txt)

    def tearDown(self):
        step13_txt = '----step13: 还原参数----'
        self.log.info(step13_txt)
        set_result = self.pri_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"ignore_standby_lsn_window="
                                               f"{self.init_para}")
        step14_txt = '----step14: 重启数据库; expect: 成功----'
        self.log.info(step14_txt)
        restart_result = self.pri_sh.restart_db_cluster()

        self.log.info(f'----{os.path.basename(__file__)}: end----')
        self.assertTrue(set_result, '执行失败:' + step13_txt)
        self.assertTrue(restart_result, '执行失败:' + step14_txt)
